#include "CGPTaskMission.h"

#define MISSION_TASK_NULL			0
#define MISSION_TASK_QUERY			1
#define MISSION_TASK_AWARD			2

CGPTaskMission::CGPTaskMission(const char* url, int port)
	: CSocketMission(url, port)
{
	mIGPTaskMissionSink = 0;
	mMissionType = MISSION_TASK_NULL;
}

// 设置回调接口
void CGPTaskMission::setMissionSink(IGPTaskMissionSink* pIGPTaskMissionSink)
{
	mIGPTaskMissionSink = pIGPTaskMissionSink;
}

// 修改登陆密码
void CGPTaskMission::QueryTask()
{
	mMissionType = MISSION_TASK_QUERY;
	start();
}

// 修改银行柜密码
void CGPTaskMission::AwardTask(CMD_GP_GetAwardTask* pNetInfo)
{
	memcpy(&taskAwardInfo, &pNetInfo, sizeof(taskAwardInfo));
	mMissionType = MISSION_TASK_AWARD;
	start();
}

void CGPTaskMission::onEventTCPSocketLink()
{
	PLAZZ_PRINTF("CGPTaskMission::onEventTCPSocketLink\n");
	//变量定义
	CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance();
	tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData();

	switch (mMissionType)
	{		// 查询
	case MISSION_TASK_QUERY:
	{
		CMD_GP_GetTaskInfo kNetInfo;
		kNetInfo.dwUserID = pGlobalUserData->dwUserID;
		send(MDM_GP_USER_SERVICE, SUM_GP_QUERY_TASK, &kNetInfo, sizeof(CMD_GP_GetTaskInfo));
		break;
	}
	case MISSION_TASK_AWARD:
	{
		send(MDM_GP_USER_SERVICE, SUM_GP_AWARD_TASK, &taskAwardInfo, sizeof(CMD_GP_GetAwardTask));
		break;
	}
	}
}

void CGPTaskMission::onEventTCPSocketShut()
{
	PLAZZ_PRINTF("CGPTaskMission::onEventTCPSocketShut\n");
}

void CGPTaskMission::onEventTCPSocketError(int errorCode)
{
	PLAZZ_PRINTF("CGPTaskMission::onEventTCPSocketError code[%d]\n", errorCode);
}

bool CGPTaskMission::onEventTCPSocketRead(int main, int sub, void* data, int size)
{
	PLAZZ_PRINTF("CGPTaskMission::onEventTCPSocketRead main:%d sub:%d size:%d\n", main, sub, size);
	if (main != MDM_GP_USER_SERVICE)
	{
		return false;
	}
	switch (sub)
	{
		//查询任务信息
	case SUM_GP_QUERY_TASK:			return onSubQueryInfoResult(data, size);
		//领取任务结果
	case SUM_GP_AWARD_TASK:			return onSubAwardResult(data, size);
		//操作成功
	case SUB_GP_OPERATE_SUCCESS:	return onSubOperateSuccess(data, size);
		//操作失败
	case SUB_GP_OPERATE_FAILURE:	return onSubOperateFailure(data, size);
	}
	return false;
}

// 查询任务信息
bool CGPTaskMission::onSubQueryInfoResult(void* data, int size)
{
	datastream kDataStream(data, size);
	DBO_GP_UserTaskInfo kNetInfo;
	kNetInfo.StreamValue(kDataStream, false);

	stop();

	if (mIGPTaskMissionSink)
		mIGPTaskMissionSink->onGPTaskQuerySuccess(&kNetInfo);

	return true;
}

// 领取任务结果
bool CGPTaskMission::onSubAwardResult(void* data, int size)
{
	PLAZZ_PRINTF("CGPTaskMission::onSubAwardResult");

	if (size != sizeof(DBO_GP_TaskAwardResult))
	{
		ASSERT(false);
		return false;
	}

	DBO_GP_TaskAwardResult* pNetInfo = (DBO_GP_TaskAwardResult*)data;

	// 	CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance();
	// 	tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData();
	// 	if (pNetInfo->bSuccessed)
	// 	{
	// 		pGlobalUserData->lUserScore = pNetInfo->lScore;
	// 		pGlobalUserInfo->upPlayerInfo();
	// 	}

	stop();

	if (mIGPTaskMissionSink)
		mIGPTaskMissionSink->onGPTaskAwardResult(pNetInfo);
	return true;
}

//操作成功
bool CGPTaskMission::onSubOperateSuccess(void* data, int size)
{
	////变量定义
	CMD_GP_OperateSuccess * pOperateSuccess = (CMD_GP_OperateSuccess *)data;

	//效验数据
	ASSERT(size >= (sizeof(CMD_GP_OperateSuccess) - sizeof(pOperateSuccess->szDescribeString)));
	if (size < (sizeof(CMD_GP_OperateSuccess) - sizeof(pOperateSuccess->szDescribeString))) return false;

	//变量定义
	CGlobalUserInfo * pGlobalUserInfo = CGlobalUserInfo::GetInstance();
	tagGlobalUserData * pGlobalUserData = pGlobalUserInfo->GetGlobalUserData();

	//关闭连接
	stop();

	//显示消息
	if (mIGPTaskMissionSink)
		mIGPTaskMissionSink->onTaskOperatorSuccess(mMissionType, pOperateSuccess->szDescribeString);

	return true;
}

bool CGPTaskMission::onSubOperateFailure(void* data, int size)
{
	PLAZZ_PRINTF("CGPTaskMission::onSubOperateFailure\n");
	//效验参数
	CMD_GP_OperateFailure * pOperateFailure = (CMD_GP_OperateFailure *)data;
	ASSERT(size >= (sizeof(CMD_GP_OperateFailure) - sizeof(pOperateFailure->szDescribeString)));
	if (size < (sizeof(CMD_GP_OperateFailure) - sizeof(pOperateFailure->szDescribeString))) return false;

	//关闭连接
	stop();

	//显示消息
	if (mIGPTaskMissionSink)
		mIGPTaskMissionSink->onTaskOperatorFailure(mMissionType, pOperateFailure->szDescribeString);

	return true;
}